package com.example.parkinglot.webController;

import com.example.parkinglot.model.CURDResult;
import com.example.parkinglot.model.PageResult;
import com.example.parkinglot.model.ParkingSpaceOrder;

import com.example.parkinglot.service.ParkingSpaceOrderService;
import com.sun.media.sound.SoftLowFrequencyOscillator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/parkingSpaceOrder")
public class parkingSpaceOrderController {
    @Resource
    ParkingSpaceOrderService orderService;
    @RequestMapping("/list")
    public String list(){ return "ParkingSpaceOrder/list";
    }
    @RequestMapping("/add")
    public String add(){ return "ParkingSpaceOrder/add";
    }
    @RequestMapping("/edit")
    public String edit(Model model,String order_id){
        ParkingSpaceOrder order=orderService.findByOrderId(order_id);
        model.addAttribute("order",order);
        System.out.println("修改订单好="+order_id);

        return "ParkingSpaceOrder/edit";
    }
    @RequestMapping("/detail")
    public String detail(Model model,String order_id){
        ParkingSpaceOrder order=orderService.findByOrderId(order_id);
        model.addAttribute("order",order);
        return "ParkingSpaceOrder/detail";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public CURDResult delete(String order_id){
        CURDResult result=new CURDResult();
        orderService.deleteByOrderId(order_id);
        return result;
    }
    @RequestMapping("/save")
    @ResponseBody
    public CURDResult save(ParkingSpaceOrder order){

        CURDResult result=new CURDResult();
        System.out.println("开始");
        System.out.println(order.getId());
        System.out.println(order.getUpdate_datetime());
        System.out.println(order.getOrder_id());
        if(order.getOrder_id()==null || order.getOrder_id().length()==0){
            orderService.save(order);
            System.out.println("保存");
        }else {
            orderService.update(order);
            System.out.println("更新");
        }


        return result;
    }
    @RequestMapping("/listJson")
    @ResponseBody
    public PageResult<ParkingSpaceOrder> listJson(int page,int limit) {
       // PageResult<ParkingSpaceOrder> result = orderService.findPageResult(null,1,8);
        PageResult<ParkingSpaceOrder> result= orderService.findPageResult(null,page,limit);
        return result;
        /*new PageResult<ParkingSpaceOrder>();
        result.setCode(0);
        result.setCount(300);
        List<ParkingSpaceOrder> list =new ArrayList<ParkingSpaceOrder>();
        result.setData(list);
        ParkingSpaceOrder order1=new ParkingSpaceOrder();
        ParkingSpaceOrder order2=new ParkingSpaceOrder();
        order1.setUser_name("springboot");
        order1.setPrice("399");
        order1.setName("CarOwner");
        list.add(order1);
        order2.setUser_name("springboot111");
        order2.setPrice("299");
        order2.setName("CarOwner111");
        list.add(order2);*/

    }
}
